<?php
//信息分类控制器
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Org\Upload;


class FriendlinkController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $db = \DB::table("friendlink");
		$list = $db->paginate(5); //5条每页浏览
        
        //处理信息
        return view("admin.friendlink.index",['list'=>$list]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
		return view("admin.friendlink.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
		if(!isset($_POST['linkname'])){
			$info = "请填写网站名称！";
			 return view("admin.friendlink.info",['info'=>$info]);
		}
		if(!isset($_POST['url'])){
			$info = "请填写网址！";
			return view("admin.friendlink.info",['info'=>$info]);
		}
		if(!isset($_POST['div'])){
			$info = "请填写模块号！";
		}
		//实例化上传对象
		if($_FILES['myfile']['error']==4){
			$info = "请上传网站LOGO！";
			return view("admin.friendlink.info",['info'=>$info]);
		}else{
			$upfile = new Upload("myfile");
	
			//初始化上传信息
			$upfile->path="./img/home/"; //上传储存路径
			$upfile->typeList =array("image/jpeg","image/png","image/gif"); //设置允许上传类型
			$upfile->maxSize =0; //允许上传大小
	
			//执行文件上传
			$res = $upfile->doUpload();
	
			//判断输出
			if($res){
				
				//执行数据库添加
				$pic = $upfile->saveName;
				$linkname = $_POST['linkname'];
				$url = $_POST['url'];
				$div = $_POST['div'];
				$id = \DB::table('friendlink')->insertGetId(['linkname' => $linkname,'url' => $url,'div'=>$div,'pic' => $pic]);
				if($id>=1){
					$db = \DB::table("friendlink");
					$list = $db->paginate(5); //5条每页浏览
				}else{
					$info = "添加失败！";
					return view("admin.friendlink.info",['info'=>$info]);
				}
			
				//处理信息
				return view("admin.friendlink.index",['list'=>$list]);
			}else{
				$info = "上传失败！原因：".$upfile->error;
				return view("admin.friendlink.info",['info'=>$info]);
				}
		}
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $list = \DB::table('friendlink')->where('id',$id)->first();
		return view("admin.friendlink.edit",['list'=>$list]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //实例化上传对象
		if($_FILES['myfile']['error']==4){
			$linkname = $_POST['linkname'];
			$url = $_POST['url'];
			$div = $_POST['div'];
			$id = \DB::table('friendlink')->where('id', $id)->update(['linkname' => $linkname,'url' => $url,'div'=>$div]);
			if($id>=1){
				return redirect("admin/friendlink");
			}else{
				$info = "修改失败！";
				return view("admin.friendlink.info",['info'=>$info]);
			}
		}else{
			$upfile = new Upload("myfile");
	
			//初始化上传信息
			$upfile->path="./img/home/"; //上传储存路径
			$upfile->typeList =array("image/jpeg","image/png","image/gif"); //设置允许上传类型
			$upfile->maxSize =0; //允许上传大小
	
			//执行文件上传
			$res = $upfile->doUpload();
	
			//判断输出
			if($res){
				
				//执行数据库添加
				$pic = $upfile->saveName;
				$linkname = $_POST['linkname'];
				$url = $_POST['url'];
				$div = $_POST['div'];
				$id = \DB::table('friendlink')->where('id', $id)->update(['linkname' => $linkname,'pic' => $pic,'url' => $url,'div'=>$div]);
				if($id>=1){
					return redirect("admin/friendlink");
				}else{
					$info = "修改失败！";
					return view("admin.friendlink.info",['info'=>$info]);
				}
			}else{
				$info = "上传失败！原因：".$upfile->error;
				return view("admin.friendlink.info",['info'=>$info]);
				}
		}
	}

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
		\DB::table('friendlink')->delete($id);
		return redirect("admin/friendlink");
    }
}
